package leetcode_2301_2400;

import java.util.Arrays;

public class LeeCode_2389 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(answerQueries(new int[]{4, 5, 2, 1}, new int[]{3, 10, 21})));
        System.out.println(Arrays.toString(answerQueries(new int[]{736411,184882,914641,37925,214915},
                new int[]{331244,273144,118983,118252,305688,718089,665450})));
    }
    private static int[] answerQueries(int[] nums, int[] queries) {
        Arrays.sort(nums);
        int[] res = new int[queries.length];
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            nums[i] = sum;
        }
        for (int i = 0; i < queries.length; i++) {
            int goal = queries[i];
            int l = 0, r = nums.length - 1;
            for (int j = nums.length - 1; j >= 0; j--) {
                if (nums[j] <= goal){
                    res[i] = j + 1;
                    break;
                }
            }
        }
        return res;
    }
}
